package misc;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * A simple interface for building persistent objects on platforms where serialization is not available.
 */

public abstract class Persistent {

	public static final byte NULL = -1;

	/**
	 * ID
	 */
	private short id;

	/**
	 * Called to get an id's Persistent instance.
	 */
	public final short getId() {
		return id;
	}

	/**
	 * Called to set an id's Persistent instance.
	 */
	public final void setId(final short i) {
		this.id = i;
	}

	public final int hashCode() {
		final int aPRIME = 31;
		int result = 1;
		result = aPRIME * result + getId();
		return result;
	}

	public final boolean equals(final Persistent object) {
		return object != null && this.getClass().equals(object.getClass()) && this.id == object.id;
	}

	/**
	 * Called to persist an object.
	 */
	public abstract void persist(final DataOutputStream dout, final boolean complete) throws IOException;

	/**
	 * Called to resurrect a persistent object.
	 */
	public abstract void resurrect(final DataInputStream din, final boolean complete) throws IOException;

}
